package com.github.baichuan.application.scaffold.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.baichuan.application.scaffold.domain.application.ApplicationService;
import com.github.baichuan.application.scaffold.domain.authorization.ActionService;
import com.github.baichuan.application.scaffold.domain.authorization.MenuService;
import com.github.baichuan.application.scaffold.domain.authorization.Resource;
import com.github.baichuan.application.scaffold.domain.authorization.ResourceService;
import com.github.baichuan.application.scaffold.dto.resource.TreeMapper;
import com.github.baichuan.application.scaffold.dto.resource.TreeNode;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.servlet.Restful;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "资源")
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ObjectMapper jsonMapper;

    @GetMapping("/tree")
    @Operation(summary = "获取资源树")
    public ResponseEntity<List<TreeNode>> tree(@RequestHeader("x-application-code") @Parameter(hidden = true) String application){
        List<Resource> resources = resourceService.findByApplication(application);
        TreeMapper treeMapper = new TreeMapper();
        List<TreeNode> views = treeMapper.map(resources);
        return ResponseEntity.ok(views);
    }

    @GetMapping("/assignable")
    @Operation(summary = "用户可授权的资源树")
    public ResponseEntity<List<TreeNode>> assignable(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                                     @RequestHeader("x-member-uid") String memberUid){
        List<Resource> resources = resourceService.getAssignable(application, memberUid);
        TreeMapper treeMapper = new TreeMapper();
        List<TreeNode> tree = treeMapper.map(resources);
        return ResponseEntity.ok(tree);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseEntity<Result> remove(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                         @PathVariable("id") String id) {
        Result result = resourceService.remove(application, id);
        return Restful.response(result);
    }

//    @Operation(summary = "获取表单数据")
//    @GetMapping("/{id}")
//    public ResponseEntity get(@PathVariable String id){
//        Resource resource = resourceService.find(id);
//        ResourceForm form = BeanMapper.map(resource, ResourceForm.class);
//        return ResponseEntity.ok(form);
//    }

//    @PostMapping
//    @Operation(summary = "添加")
//    public ResponseEntity add(@RequestHeader("x-application-code") @Parameter(hidden = true) String application, @RequestBody @Valid ResourceForm form) {
//        Result result = resourceService.add(application, form);
//        return Restful.created(result);
//    }
//
//    @PutMapping("/{id}")
//    @Operation(summary = "修改")
//    public ResponseEntity edit(@RequestHeader("x-application-code") @Parameter(hidden = true) String application, @PathVariable String id, @RequestBody @Valid ResourceForm form) {
//        Result result = resourceService.edit(application, id, form);
//        return ResponseEntity.ok(result);
//    }


}
